package com.zt.springcloud.controller;

import com.zt.springcloud.entity.Cart;
import com.zt.springcloud.entity.vo.CartVo;
import com.zt.springcloud.service.ShopCartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RedReins
 * @version 1.0
 * @description: TODO
 * @date 2021/9/19 11:10
 */

@RestController
@RequestMapping("/shopcart")
public class ShopCartController {
    private static Logger logger = LoggerFactory.getLogger(ShopCartController.class);
    @Autowired
    private ShopCartService shopCartService;

    /*添加购物车*/
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Boolean addShopCart(@RequestBody Cart cart){
        logger.info("addShopCart:{}",cart.getItemId());
        return shopCartService.insertCart(cart)>0;
    }
    /*查詢所有购物车商品*/
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<CartVo> getCartList(Long id){
        return shopCartService.getCartList(id);
    }

    /*
     * 增加购物车中商品的购买数量
     * */
    @RequestMapping(value = "/addAmount/{itemid}",method = RequestMethod.PUT)
    public Integer addShopCartAmount(@PathVariable("itemid") Integer itemid){
        return shopCartService.addShopCartAmount(itemid);
    }

    /*
     * 增加购物车中商品的购买数量
     * */
    @RequestMapping(value = "/minusAmount/{itemid}",method = RequestMethod.PUT)
    public Integer minusShopCartAmount(@PathVariable("itemid")Integer itemid){
        return shopCartService.minusShopCartAmount(itemid);
    }
    /*
     * 根据itemId查询购物车的商品
     * */
    @RequestMapping(value = "/listByIds",method = RequestMethod.POST)
    public List<CartVo> findItemsByIds(@RequestBody  String itemIds) throws Exception {
        return shopCartService.findItemsByIds(itemIds);
    }
}
